package com.wechat.wechat.service.impl;

import com.wechat.wechat.config.WechatAccountConfig;
import com.wechat.wechat.dto.OrderDTO;
import com.wechat.wechat.service.PushMessageService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
@Service
@Slf4j
public class PushMessageServiceImpl implements PushMessageService {
    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private WechatAccountConfig wechatAccountConfig;
    @Override
    public void orderStatus(OrderDTO orderDTO) {
        WxMpTemplateMessage templateMessage=new WxMpTemplateMessage();
        //消息模板id
        templateMessage.setTemplateId(wechatAccountConfig.getTemplateId().get("orderStatus"));
        //要发送的用户的openid
        templateMessage.setToUser(orderDTO.getBuyerOpenid());
        //要发送的数据，对应模板
        List<WxMpTemplateData> data= Arrays.asList(
                new WxMpTemplateData("first","亲记得收货"),
                new WxMpTemplateData("keyword1","微信点餐"),
                new WxMpTemplateData("keyword2","18622547124"),
                new WxMpTemplateData("keyword3",orderDTO.getOrderId()),
                new WxMpTemplateData("keyword4",orderDTO.getOrderStatusEnum().getMessage()),
                new WxMpTemplateData("keyword5","￥"+orderDTO.getOrderAmount()),
                new WxMpTemplateData("remark","欢迎再次光临")
        );
        templateMessage.setData(data);
        try{
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        }catch(Exception e)
        {
            log.error("【微信模板消息】发送失败，{}",e);
        }

    }
}
